package com.emily.test.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.emily.test.constant.ReturnValueConstants;
import com.emily.test.controller.result.ResponseResult;
import com.emily.test.model.TCompany;
import com.emily.test.model.TDept;
import com.emily.test.model.TRole;
import com.emily.test.model.TUser;
import com.emily.test.service.DeptService;
import com.emily.test.service.RedisService;
import com.emily.test.service.TUserService;
import com.emily.test.utils.ConstentUtil;
import com.emily.test.utils.DateUtil;
import com.emily.test.utils.HeaderUtil;
import com.emily.test.utils.MD5Utils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@CrossOrigin(origins = "*", maxAge = 3600)
@RestController
@RequestMapping(value = "tUserController")
@Api(value = "TUserApi", description = "TUser接口类", position = 1, produces = MediaType.APPLICATION_JSON_VALUE)
public class TUserController {

    private Logger logger = LoggerFactory.getLogger(TUserController.class);

    @Autowired
    private TUserService tUserService;

    @Autowired
    private RedisService redisService;

    @Autowired
    private DeptService deptService;
    /**
     * 根据条件查询用户信息
     * @param jsonObject
     * @return
     * @throws Exception
     */
    @ApiOperation(value="根据条件查询用户列表集合", notes="根据条件查询用户列表集合")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "菜单ID"),
            @ApiImplicitParam(name = "loginName", value = "登录名"),
            @ApiImplicitParam(name = "password", value = "密码"),
            @ApiImplicitParam(name = "userName", value = "用户名"),
            @ApiImplicitParam(name = "email", value = "电子邮箱"),
            @ApiImplicitParam(name = "phone", value = "联系方式"),
            @ApiImplicitParam(name = "sex", value = "性别"),
            @ApiImplicitParam(name = "age", value = "年龄"),
            @ApiImplicitParam(name = "state", value = "启用状态：1、启用；0、禁用；2、删除状态"),
            @ApiImplicitParam(name = "serial", value = "显示顺序"),
            @ApiImplicitParam(name = "companyCode", value = "公司code"),
            @ApiImplicitParam(name = "description", value = "备注"),
            @ApiImplicitParam(name = "createTime", value = "创建时间"),
            @ApiImplicitParam(name = "updateTime", value = "修改时间")
    })
    @PostMapping(value = "getUserInfoListByCondition")
    public ResponseResult getUserInfoListByCondition(@RequestBody(required = false) JSONObject jsonObject,HttpServletRequest request) throws Exception {
        TUser tUser= (TUser) ConstentUtil.json2Object(jsonObject,TUser.class);
        String token = request.getHeader("token");
        Map<String,Object> userMap= (Map<String, Object>) redisService.get(token);
        String companyCode=null;
        List<Map<String,Object>> userRoleList= (List<Map<String, Object>>) userMap.get("userRoleList");

        //判断是否为超管，如果是，则查询所有用户信息，否则查询本公司用户信息
        boolean flag=false;
        if(!ConstentUtil.isEmpty(userRoleList)){
            for(Map<String,Object> roleMap : userRoleList){
                String roleCode= (String) roleMap.get("roleCode");
                //如果是超管
                if("SUPERADMIN".equals(roleCode)){
                    flag=true;
                    break;
                }
            }
        }

        try {
            List<TUser> tUserList =new ArrayList<TUser>();
            //如果为超管登陆，则查询所有用户信息
            if(flag){
                TUser tu=new TUser();
//                tu.setState(1);
                tUserList=tUserService.getUserInfoListByCondition(tu);
            }else{
                if(!ConstentUtil.isEmpty(jsonObject)){
                    if(!ConstentUtil.isEmpty(jsonObject.getString("companyCode"))){
                        companyCode=jsonObject.getString("companyCode");
                    }
                }
                if(!ConstentUtil.isEmpty(userMap)){
                    if(!ConstentUtil.isEmpty(userMap.get("companyCode"))){
                        companyCode= userMap.get("companyCode") == null ? null : (String) userMap.get("companyCode");
                    }
                }
                tUser.setCompanyCode(companyCode);
                tUserList= tUserService.getUserInfoListByCondition(tUser);
            }
            logger.info("根据条件查询用户列表集合成功,集合为:{}",tUserList);
            return ResponseResult.success(tUserList);
             } catch (Exception e) {
                 logger.info("根据条件查询用户列表集合失败");
                 e.printStackTrace();
                 return ResponseResult.fail("查询失败");
        }
    }

    /**
     * 查询仍未配置角色用户信息列表
     * @return
     */
    @ApiOperation(value="查询仍未配置角色的用户信息列表", notes="Tuser")
    @PostMapping(value = "getUserListNoRoles")
    public ResponseResult getUserListNoRoles(HttpServletRequest request){
        try {
            List<TUser> resourceList=tUserService.getUserListNoRoles();
            String code = HeaderUtil.getCompanyCode(request,redisService);
            for(int a = 0;a<resourceList.size();a++){
                if(resourceList.get(a).getCompanyCode()==null ||!resourceList.get(a).getCompanyCode().equals(code)){
                    resourceList.remove(a);
                }
            }
            logger.info("查询用户信息列表成功!resourceList={}",resourceList);
            return ResponseResult.success(ReturnValueConstants.QUERY_SUCCESS,resourceList);
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("查询用户信息列表异常:{}",e);
            return ResponseResult.fail(ReturnValueConstants.QUERY_FAILURE);
        }
    }

    /**
     * 用户注册
     * @param jsonObject
     * @param request
     * @param response
     * @return
     */
    @ApiOperation(value="用户注册", notes="用户注册")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "loginName", value = "登录名",required = true),
            @ApiImplicitParam(name = "password", value = "密码",required = true),
            @ApiImplicitParam(name = "userName", value = "用户名",required = true),
            @ApiImplicitParam(name = "email", value = "电子邮箱"),
            @ApiImplicitParam(name = "phone", value = "联系方式",required = true),
            @ApiImplicitParam(name = "sex", value = "性别"),
            @ApiImplicitParam(name = "age", value = "年龄"),
            @ApiImplicitParam(name = "state", value = "启用状态：1、启用；0、禁用；2、删除状态"),
            @ApiImplicitParam(name = "serial", value = "显示顺序"),
            @ApiImplicitParam(name = "companyCode", value = "公司code"),
            @ApiImplicitParam(name = "description", value = "备注"),
            @ApiImplicitParam(name = "createTime", value = "创建时间"),
            @ApiImplicitParam(name = "updateTime", value = "修改时间")
    })
    @PostMapping(value = "userRegister")
    public ResponseResult userRegister(@RequestBody JSONObject jsonObject,
                                       HttpServletRequest request, HttpServletResponse response) throws Exception {

        TUser tUser= (TUser) ConstentUtil.json2Object(jsonObject,TUser.class);
        //登录名不允许为空
        if(ConstentUtil.isEmpty(tUser.getLoginName())){
            return ResponseResult.fail("登录名"+ReturnValueConstants.IS_NOT_ALLOW_NULL);
        }
        //密码不能为空
        if(ConstentUtil.isEmpty(tUser.getPassword())){
            return ResponseResult.fail("密码"+ ReturnValueConstants.IS_NOT_ALLOW_NULL);
        }
        //用户名不能为空
        if(ConstentUtil.isEmpty(tUser.getUserName())){
            return ResponseResult.fail("用户名"+ReturnValueConstants.IS_NOT_ALLOW_NULL);
        }
        //电话不能为空
        if(ConstentUtil.isEmpty(tUser.getPhone())){
            return ResponseResult.fail("电话"+ReturnValueConstants.IS_NOT_ALLOW_NULL);
        }

        TUser tUser1=new TUser();
        tUser1.setLoginName(tUser.getLoginName());
        //登录名要求单一
        if(!ConstentUtil.isEmpty(tUserService.selectIsExistByCondition(tUser1))){
            return ResponseResult.fail("登录名"+ReturnValueConstants.IS_HAVE_EXIST);
        }
        tUser1.setLoginName(null);
        tUser1.setPhone(tUser.getPhone());
        //手机要求单一
        if(!ConstentUtil.isEmpty(tUserService.selectIsExistByCondition(tUser1))){
            return ResponseResult.fail("手机号码"+ReturnValueConstants.IS_HAVE_EXIST);
        }
        //如果注册时邮箱不为空，则进行判断邮箱是否已经注册
        if(!ConstentUtil.isEmpty(tUser.getEmail())){
            tUser1.setPhone(null);
            tUser1.setEmail(tUser.getEmail());
            if(!ConstentUtil.isEmpty(tUserService.selectIsExistByCondition(tUser1))){
                return ResponseResult.fail("邮箱"+ReturnValueConstants.IS_HAVE_EXIST);
            }
        }
        try {
                 tUser.setCreateTime(DateUtil.getNowStr());
                 tUser.setPassword(MD5Utils.getEncryptedPwd(tUser.getPassword()));
                 int num = tUserService.insertUser(tUser);
                 logger.info("新用户"+ReturnValueConstants.ADD_SUCCESS+",请联系管理员配置权限");
                 return ResponseResult.success("新用户"+ReturnValueConstants.ADD_SUCCESS+",请联系管理员配置权限",tUser);
             } catch (Exception e) {
                logger.error("新用户"+ReturnValueConstants.ADD_FAILURE);
                 e.printStackTrace();
                 return ResponseResult.fail(ReturnValueConstants.ADD_FAILURE);
        }
    }

    /**
     * 修改用户信息
     * @param jsonObject
     * @return
     * @throws Exception
     */
    @ApiOperation(value="用户信息更新", notes="用户信息更新")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "用户ID",required = true),
            @ApiImplicitParam(name = "loginName", value = "登录名"),
            @ApiImplicitParam(name = "loginName", value = "登录名"),
            @ApiImplicitParam(name = "password", value = "密码"),
            @ApiImplicitParam(name = "userName", value = "用户名"),
            @ApiImplicitParam(name = "email", value = "电子邮箱"),
            @ApiImplicitParam(name = "phone", value = "联系方式"),
            @ApiImplicitParam(name = "sex", value = "性别"),
            @ApiImplicitParam(name = "age", value = "年龄"),
            @ApiImplicitParam(name = "state", value = "启用状态：1、启用；0、禁用；2、删除状态"),
            @ApiImplicitParam(name = "serial", value = "显示顺序"),
            @ApiImplicitParam(name = "companyCode", value = "公司code"),
            @ApiImplicitParam(name = "description", value = "备注"),
            @ApiImplicitParam(name = "createTime", value = "创建时间"),
            @ApiImplicitParam(name = "updateTime", value = "修改时间")
    })
    @PostMapping(value = "updateUserInfoByUserId")
    public ResponseResult updateUserInfoByUserId(@RequestBody JSONObject jsonObject) throws Exception {
        TUser tUser= (TUser) ConstentUtil.json2Object(jsonObject,TUser.class);
        if(ConstentUtil.isEmpty(tUser.getId())){
            return ResponseResult.fail("ID"+ReturnValueConstants.IS_NOT_ALLOW_NULL);
        }
        TUser us=tUserService.getUserInfoById(tUser.getId());
        //如果密码不为空，则判断是否是修改密码
        if(!ConstentUtil.isEmpty(tUser.getPassword())){
            TUser tUser1=new TUser();
            //如果密码与数据库的密码不一致，则判断是否为加密后的
            if(!(tUser.getPassword()).equals(us.getPassword())){
                if(!MD5Utils.validPassword(tUser.getPassword(),us.getPassword())){
                    tUser.setPassword(MD5Utils.getEncryptedPwd(tUser.getPassword()));
                }
            }
        }
        //如果是修改手机号码,首先查询下修改后的手机号是否已经被注册
        if(!ConstentUtil.isEmpty(tUser.getPhone())){
            if(!(tUser.getPhone()).equals(us.getPhone())){
                TUser ts=new TUser();
                ts.setId(tUser.getId());
                ts.setPhone(tUser.getPhone());
                int num =tUserService.findNumByPhoneWithOutId(ts);
                if(num>0){
                    return ResponseResult.fail("手机号"+ReturnValueConstants.IS_HAVE_EXIST);
                }
            }
        }
        tUser.setUpdateTime(DateUtil.getNowStr());
        try {
                 int num=tUserService.updateUserInfoByUserId(tUser);
                 logger.info("用户信息"+ReturnValueConstants.UPDATE_SUCCESS);
                 return ResponseResult.success(tUser);
             } catch (Exception e) {
                 logger.info("用户信息"+ReturnValueConstants.UPDATE_FAILURE);
                 e.printStackTrace();
                 return ResponseResult.fail(ReturnValueConstants.UPDATE_FAILURE);
        }
    }



    /**
     * 查询还没有公司的用户信息列表
     * @return
     */
    @ApiOperation(value="查询没有公司的用户列表", notes="查询没有公司的用户列表")
    @PostMapping(value = "getUserListWithOutCompany")
    public ResponseResult getUserListWithOutCompany(@RequestBody(required = false) JSONObject jsonObject){
        String userName=null;
        if(!ConstentUtil.isEmpty(jsonObject)){
           userName=jsonObject.getString("userName");
        }
        TUser tUser=new TUser();
        tUser.setUserName(userName);
        try {
            List<TUser> userList    =   tUserService.getUserListWithOutCompany(tUser);
            logger.info("查询没有公司的用户列表成功，列表:{}",userList);
            return ResponseResult.success(userList);
        } catch (Exception e) {
            logger.error("查询异常",e);
            e.printStackTrace();
            return ResponseResult.fail("查询失败!");
        }
    }

    /**
     * 将用户从公司中去除
     * @param jsonObject
     * @return
     * @throws Exception
     */
    @ApiOperation(value="将用户从公司中去除", notes="用户信息更新")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ids", value = "用户IDs",required = true)
    })
    @PostMapping(value = "removeUserFromCompany")
    public ResponseResult removeUserFromCompany(@RequestBody JSONObject jsonObject) throws Exception {
        String ids=jsonObject.getString("ids").replaceAll("\\[","").replaceAll("]","");
        String[] idStr=ids.split(",");
        List<Integer> userIdList=new ArrayList<Integer>();
        if(idStr.length !=0){
            for( int i=0;i<idStr.length;i++){
                userIdList.add(ConstentUtil.strToInteger(idStr[i]));
            }
        }
        try {
                 int num=tUserService.removeUserFromCompanyByUserIds(userIdList);
                 logger.info("去除成功！去除的用户id:{}",userIdList);
                 return ResponseResult.success(num);
             } catch (Exception e) {
                 logger.info("去除异常！");
                 e.printStackTrace();
                 return ResponseResult.fail("去除失败！");
        }
    }
    /**
     * 启用多个用户
     * @param jsonObject    其中用户ids的格式为:ids={1,2,3,4}
     * @return
     */
    @ApiOperation(value="启用多个用户", notes="启用多个用户")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ids", value = "用户ID列表", required = true)
    })
    @PostMapping(value = "enableUserByUserIds")
    public ResponseResult enableUserByUserIds(@RequestBody JSONObject jsonObject){
        String ids=jsonObject.getString("ids").replaceAll("\\[","").replaceAll("]","");
        String[] idStr=ids.split(",");
        List<TUser> tUserList=new ArrayList<TUser>();
        if(idStr.length !=0){
            for( int i=0;i<idStr.length;i++){
                TUser tUser=new TUser();
                tUser.setId(ConstentUtil.strToInteger(idStr[i]));
                tUser.setState(1);
                tUser.setUpdateTime(DateUtil.getNowStr());
                tUserList.add(tUser);
            }
        }
        try {
            int num= tUserService.updateUserByIds(tUserList);
            logger.info("ids为{}的用户已启用,ids={}",ids);
            return ResponseResult.success("0","启用成功",tUserList);
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseResult.fail("启用失败");
        }
    }

    /**
     * 禁用多个用户
     * @param jsonObject    其中用户ids的格式为:ids={1,2,3,4}
     * @return
     */
    @ApiOperation(value="禁用多个用户", notes="禁用多个用户")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ids", value = "用户ID列表", required = true)
    })
    @PostMapping(value = "disableUserByUserIds")
    public ResponseResult disableUserByUserIds(@RequestBody JSONObject jsonObject){
        String ids=jsonObject.getString("ids").replaceAll("\\[","").replaceAll("]","");
        String[] idStr=ids.split(",");
        List<TUser> tUserList=new ArrayList<TUser>();
        if(idStr.length !=0){
            for( int i=0;i<idStr.length;i++){
                TUser tUser=new TUser();
                tUser.setId(ConstentUtil.strToInteger(idStr[i]));
                tUser.setState(0);
                tUser.setUpdateTime(DateUtil.getNowStr());
                tUserList.add(tUser);
            }
        }
        try {
            int num= tUserService.updateUserByIds(tUserList);
            logger.info("ids为{}的用户已禁用:ids={}",ids);
            return ResponseResult.success("0","禁用成功",tUserList);
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseResult.fail("禁用失败");
        }
    }

    /**
     * 批量将用户添加到公司
     * @param jsonObject 其中包括ids=[1,2,3,4,...],以及字符串参数:companyName,companyCode
     * @return
     */
    @ApiOperation(value="批量将用户添加到公司", notes="批量将用户添加到公司")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ids", value = "用户ID列表", required = true),
            @ApiImplicitParam(name = "companyName", value = "公司名称", required = true),
            @ApiImplicitParam(name = "companyCode", value = "公司code", required = true)
    })
    @PostMapping(value = "putUsersInCompany")
    public ResponseResult putUsersInCompany(@RequestBody JSONObject jsonObject,HttpServletRequest request){
        String ids=jsonObject.getString("ids").replaceAll("\\[","").replaceAll("]","");

        //后续需要获取公司code跟公司名称

        String token = request.getHeader("token");
        Map<String,Object> userMap= (Map<String, Object>) redisService.get(token);
        String companyCode=null;
        String companyName=null;
        if(!ConstentUtil.isEmpty(jsonObject)){
            if(!ConstentUtil.isEmpty(jsonObject.getString("companyCode"))){
                companyCode=jsonObject.getString("companyCode");
            }
            if(!ConstentUtil.isEmpty(jsonObject.getString("companyName"))){
                companyName=jsonObject.getString("companyName");
            }
        }
        if(!ConstentUtil.isEmpty(userMap)){
            if(!ConstentUtil.isEmpty(userMap.get("companyCode"))){
                companyCode= userMap.get("companyCode") == null ? null : (String) userMap.get("companyCode");
            }
            if(!ConstentUtil.isEmpty(userMap.get("companyName"))){
                companyName= userMap.get("companyName") == null ? null : (String) userMap.get("companyName");
            }
        }
//        tUser.setCompanyCode(companyCode);
        String[] idStr=ids.split(",");
        List<TUser> tUserList=new ArrayList<TUser>();
        if(idStr.length !=0){
            for( int i=0;i<idStr.length;i++){
                TUser tUser=new TUser();
                tUser.setUpdateTime(DateUtil.getNowStr());
                tUser.setId(ConstentUtil.strToInteger(idStr[i]));
                tUser.setCompanyName(companyName);
                tUser.setCompanyCode(companyCode);
                tUserList.add(tUser);
            }
        }
        try {
                 int num=tUserService.updateUserByIds(tUserList);
                 logger.info("批量将用户添加到公司成功,添加list={}",tUserList);
                 return ResponseResult.success(tUserList);
             } catch (Exception e) {
                logger.info("批量将用户添加到公司异常",e);
                 e.printStackTrace();
                return ResponseResult.fail("添加异常");
        }
    }


    /**
     * 将单个用户添加到公司某个部门下
     * @param jsonObject 其中包括userId,deptCode,deptName
     * @return
     */
    @ApiOperation(value="将单个用户添加到公司", notes="将单个用户添加到公司")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "用户ID", required = true),
//            @ApiImplicitParam(name = "companyName", value = "公司名称", required = true),
//            @ApiImplicitParam(name = "companyCode", value = "公司code", required = true),
            @ApiImplicitParam(name = "deptCode", value = "部门code", required = true),
            @ApiImplicitParam(name = "deptName", value = "部门名称", required = true)
    })
    @PostMapping(value = "putSingleUserInCompany")
    public ResponseResult putSingleUserInCompany(@RequestBody JSONObject jsonObject,HttpServletRequest request){
        String userId=jsonObject.getString("userId").replaceAll("\\[","").replaceAll("]","");
        String deptCode=jsonObject.getString("deptCode");
        String deptName=jsonObject.getString("deptName");
        //后续需要获取公司code跟公司名称

        String token = request.getHeader("token");
        Map<String,Object> userMap= (Map<String, Object>) redisService.get(token);
        String companyCode=null;
        String companyName=null;
        if(!ConstentUtil.isEmpty(jsonObject)){
            if(!ConstentUtil.isEmpty(jsonObject.getString("companyCode"))){
                companyCode=jsonObject.getString("companyCode");
            }
            if(!ConstentUtil.isEmpty(jsonObject.getString("companyName"))){
                companyName=jsonObject.getString("companyName");
            }
        }
        if(!ConstentUtil.isEmpty(userMap)){
            if(!ConstentUtil.isEmpty(userMap.get("companyCode"))){
                companyCode= userMap.get("companyCode") == null ? null : (String) userMap.get("companyCode");
            }
            if(!ConstentUtil.isEmpty(userMap.get("companyName"))){
                companyName= userMap.get("companyName") == null ? null : (String) userMap.get("companyName");
            }
        }
        List<TUser> tUserList=new ArrayList<TUser>();
        TUser tUser=new TUser();
        tUser.setUpdateTime(DateUtil.getNowStr());
        tUser.setId(ConstentUtil.strToInteger(userId));
        tUser.setCompanyName(companyName);
        tUser.setCompanyCode(companyCode);
        tUser.setDeptCode(deptCode);
        tUser.setDeptName(deptName);
        tUserList.add(tUser);
        try {
            int num=tUserService.updateUserByIds(tUserList);
            logger.info("批量将用户添加到公司成功,添加list={}",tUserList);
            return ResponseResult.success(tUserList);
        } catch (Exception e) {
            logger.info("批量将用户添加到公司异常",e);
            e.printStackTrace();
            return ResponseResult.fail("添加异常");
        }
    }

    /**
     * 根据用户ID获取用户信息以及所有的部门信息集合，其中用户已经有的部门信息添加selected=1属性，没有的则为selected=0.
     * @param jsonObject
     * @return
     */
    @ApiOperation(value="根据用户ID获取用户信息以及所有的部门信息集合，" +
            "其中用户已经有的部门信息添加selected=1属性，没有的则为selected=0.", notes="t_user")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "用户ID", required = true)
    })
    @PostMapping(value = "getUserAndAllDeptInfoByUserId")
    public ResponseResult getUserAndAllDeptInfoByUserId(@RequestBody JSONObject jsonObject,HttpServletRequest request){
        try {
            //用户ID
            Integer userId=jsonObject.getInteger("id");

            //根据用户ID获取用户信息
            TUser tUser= tUserService.getUserInfoById(userId);
            //获取所有启用的部门信息
            List<TDept> tDeptList=deptService.getDeptList(HeaderUtil.getCompanyCode(request,redisService));

            Map<String,Object> returnMapList=new HashMap<String,Object>();
            //将用户信息放入map
            returnMapList.put("id",tUser.getId());
            returnMapList.put("userName",tUser.getUserName());
            returnMapList.put("loginName",tUser.getLoginName());
            returnMapList.put("password",tUser.getPassword());
            returnMapList.put("phone", tUser.getPhone());

            //将角色信息放入集合
            List<Map<String,Object>> inputList=new ArrayList<Map<String,Object>>();
            if(!ConstentUtil.isEmpty(tDeptList)){
                for(TDept tDept : tDeptList){

                    String deptCode=tDept.getDeptCode();

                    Map<String,Object> roleMap=new HashMap<>();
                    roleMap.put("deptId",tDept.getId());
                    roleMap.put("deptName",tDept.getDeptName());
                    roleMap.put("deptPid",tDept.getPid());
                    roleMap.put("deptCode",tDept.getDeptCode());
                    roleMap.put("companyCode",tDept.getCompanyCode());
                    roleMap.put("deptState",tDept.getState());
                    roleMap.put("deptUpdateTime",tDept.getUpdateTime());
                    roleMap.put("deptDescription",tDept.getDescription());

                    if(!ConstentUtil.isEmpty(tUser.getDeptCode())){
                            if((tUser.getDeptCode()).equals(deptCode)){
                                roleMap.put("selected","1");
                            }else{
                                roleMap.put("selected","0");
                            }
                    }else{
                        roleMap.put("selected","0");
                    }
                    inputList.add(roleMap);
                }
            }
            returnMapList.put("deptList",inputList);
            logger.info("查询用户角色信息成功:{}",returnMapList);
            return ResponseResult.success(returnMapList);
        } catch (Exception e) {
            logger.error("查询用户角色信息异常",e);
            e.printStackTrace();
            return ResponseResult.fail("查询失败");
        }
    }

}
